package com.lwj.liwanjia.view.activity.work.shang_jia;

import android.content.Intent;
import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.WoDeShangPingAdapter;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.MySpBean;
import com.lwj.liwanjia.bean.SjTypeBean;
import com.lwj.liwanjia.bean.fabu.XjSpBean;
import com.lwj.liwanjia.impl.DeleteSpView;
import com.lwj.liwanjia.impl.MySpView;
import com.lwj.liwanjia.impl.SjTypeView;
import com.lwj.liwanjia.impl.XjSpView;
import com.lwj.liwanjia.presenter.DeleteSpPresenter;
import com.lwj.liwanjia.presenter.MySpPresenter;
import com.lwj.liwanjia.presenter.SjTypePresenter;
import com.lwj.liwanjia.presenter.XjSpPresneter;
import com.lwj.liwanjia.utils.CancleDialog;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.view.activity.work.shang_jia.spfabu.DzSmFabuActivity;
import com.lwj.liwanjia.view.activity.work.shang_jia.spfabu.FzFabuActivity;
import com.lwj.liwanjia.view.activity.work.shang_jia.spfabu.JyFabuActivity;
import com.lwj.liwanjia.view.activity.work.shang_jia.spfabu.MuyinFabuActivity;
import com.lwj.liwanjia.view.activity.work.shang_jia.spfabu.SpFabuActivity;
import com.lwj.liwanjia.view.activity.work.shang_jia.spfabu.XhFabuActivity;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by 001 on 2017/9/13.
 */

public class ShangPingHomeActivity extends BaseActivity implements SjTypeView, DeleteSpView, XjSpView, MySpView, SuperRecyclerView.LoadingListener {


    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.recyclerView)
    SuperRecyclerView recyclerView;
    @BindView(R.id.no_fabu)
    LinearLayout noFabu;

    private WoDeShangPingAdapter adapter;
    private List<MySpBean.DataBean> list = new ArrayList<>();
    private MySpPresenter spPresenter;
    private String oneType;
    private XjSpPresneter xjSpPresneter;
    private DeleteSpPresenter deleteSpPresenter;
    private SjTypePresenter typePresenter;
    private Map<String, Object> map = new HashMap<>();
    private int clickPosition;
    private CancleDialog dialog;


    @Override
    protected void initView() {
        tvLeft.setVisibility(View.VISIBLE);
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText("发布");
        tvTitle.setText("我的商品");
        GridLayoutManager manager = new GridLayoutManager(this, 2);
        recyclerView.setLayoutManager(manager);
        recyclerView.setRefreshEnabled(true);
        recyclerView.setLoadMoreEnabled(false);
        recyclerView.setLoadingListener(this);
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.left = 10;
                outRect.bottom = 10;
                if (parent.getChildAdapterPosition(view) % 2 == 0) {
                    outRect.right = 10;
                }
            }
        });
        adapter = new WoDeShangPingAdapter(this, list);
        recyclerView.setAdapter(adapter);
        spPresenter = new MySpPresenter(this);
        xjSpPresneter = new XjSpPresneter(this);
        deleteSpPresenter = new DeleteSpPresenter(this);
        typePresenter = new SjTypePresenter(this);
        listenter();
    }

    private void listenter() {
        adapter.setOnItemClickListener(new SuperBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Object item, int position) {
                String commodityBatch = list.get(position).getCommodityBatch();
                clickListenter(commodityBatch,0);

            }
        });
        adapter.setOnItemChildClickListener(new SuperBaseAdapter.OnRecyclerViewItemChildClickListener() {
            @Override
            public void onItemChildClick(SuperBaseAdapter adapter, View view, final int position) {
                clickPosition = position;
                TextView tv = (TextView) view;
                switch (tv.getText().toString()) {
                    case "编辑":
                        clickListenter(list.get(position).getCommodityBatch(),1);
                        break;
                    case "删除":
                        dialog = new CancleDialog(ShangPingHomeActivity.this, "删除");
                        dialog.show();
                        dialog.getSure().setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                deleteSpPresenter.deleteSpData(SpfUtils.getSpfUtils(ShangPingHomeActivity.this).getToken(), list.get(position).getCommodityBatch());
                                dialog.dismiss();
                            }
                        });
                        break;
                    case "下架":
                        map.put("token", SpfUtils.getSpfUtils(ShangPingHomeActivity.this).getToken());
                        map.put("commodityBatch", list.get(position).getCommodityBatch());
                        map.put("status", "0");
                        dialog = new CancleDialog(ShangPingHomeActivity.this, "下架");
                        dialog.show();
                        dialog.getSure().setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                xjSpPresneter.XjSpData(map);
                                dialog.dismiss();
                            }
                        });
                        break;
                    case "上架":
                        map.put("token", SpfUtils.getSpfUtils(ShangPingHomeActivity.this).getToken());
                        map.put("commodityBatch", list.get(position).getCommodityBatch());
                        map.put("status", "1");
                        dialog = new CancleDialog(ShangPingHomeActivity.this, "上架");
                        dialog.show();
                        dialog.getSure().setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                xjSpPresneter.XjSpData(map);
                                dialog.dismiss();
                            }
                        });
                        break;
                }
            }
        });
    }

    private void clickListenter(String commodityBatch,int type) {
        if (!TextUtils.isEmpty(oneType) && oneType != null) {
            if (oneType.equals("服装城")) {
                startActivity(new Intent(ShangPingHomeActivity.this, FzFabuActivity.class).putExtra("commodityBatch", commodityBatch).putExtra("type",type));
            } else if (oneType.equals("食品") || oneType.equals("日常用品")) {
                startActivity(new Intent(ShangPingHomeActivity.this, SpFabuActivity.class).putExtra("commodityBatch", commodityBatch).putExtra("type",type));
            } else if (oneType.equals("家用电器")) {
                startActivity(new Intent(ShangPingHomeActivity.this, JyFabuActivity.class).putExtra("commodityBatch", commodityBatch).putExtra("type",type));
            } else if (oneType.equals("日化洗护")) {
                startActivity(new Intent(ShangPingHomeActivity.this, XhFabuActivity.class).putExtra("commodityBatch", commodityBatch).putExtra("type",type));
            } else if (oneType.equals("母婴玩具")) {
                startActivity(new Intent(ShangPingHomeActivity.this, MuyinFabuActivity.class).putExtra("commodityBatch", commodityBatch).putExtra("type",type));
            } else if (oneType.equals("电子数码")) {
                startActivity(new Intent(ShangPingHomeActivity.this, DzSmFabuActivity.class).putExtra("commodityBatch", commodityBatch).putExtra("type",type));
            }
        } else {
            Toast.makeText(ShangPingHomeActivity.this, "商家类型为空", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void initData() {
        typePresenter.getSjType(SpfUtils.getSpfUtils(this).getToken());
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_wo_de_shang_ping;
    }

    @OnClick({R.id.tv_left, R.id.tv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.tv_right://发布
                if (!TextUtils.isEmpty(oneType) && oneType != null) {
                    if (oneType.equals("服装城")) {
                        startActivity(new Intent(ShangPingHomeActivity.this, FzFabuActivity.class));
                    } else if (oneType.equals("食品") || oneType.equals("日常用品")) {
                        startActivity(new Intent(ShangPingHomeActivity.this, SpFabuActivity.class));
                    } else if (oneType.equals("家用电器")) {
                        startActivity(new Intent(ShangPingHomeActivity.this, JyFabuActivity.class));
                    } else if (oneType.equals("日化洗护")) {
                        startActivity(new Intent(ShangPingHomeActivity.this, XhFabuActivity.class));
                    } else if (oneType.equals("母婴玩具")) {
                        startActivity(new Intent(ShangPingHomeActivity.this, MuyinFabuActivity.class));
                    } else if (oneType.equals("电子数码")) {
                        startActivity(new Intent(ShangPingHomeActivity.this, DzSmFabuActivity.class));
                    } else {
                        Toast.makeText(this, "商家类型为空", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(this, "商家类型为空", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "访问网络失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getSjType(SjTypeBean sjTypeBean) {
        if (sjTypeBean.getState() == 0) {
            Toast.makeText(this, "数据加载失败", Toast.LENGTH_SHORT).show();
        } else if (sjTypeBean.getState() == 1) {
            oneType = sjTypeBean.getData().get(0).getScope();
        }
    }

    @Override
    public void deleteSpData(XjSpBean xjSpBean) {
        if (xjSpBean.getState() == 0) {
            Toast.makeText(this, "数据加载失败", Toast.LENGTH_SHORT).show();
        } else if (xjSpBean.getState() == 1) {
            if (xjSpBean.getData().getStatus().equals("删除成功")) {
                Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
                list.remove(clickPosition);
                adapter.notifyDataSetChanged();
                if (list.size() == 0) {
                    noFabu.setVisibility(View.VISIBLE);
                    recyclerView.setVisibility(View.GONE);
                }

            }
        }
    }

    /**
     * 上下架商品
     *
     * @param xjSpBean
     */
    @Override
    public void xjSpData(XjSpBean xjSpBean) {
        if (xjSpBean.getState() == 0) {
            Toast.makeText(this, "数据加载失败", Toast.LENGTH_SHORT).show();
        } else if (xjSpBean.getState() == 1) {
            if (xjSpBean.getData().getStatus().equals("1")) {
                Toast.makeText(this, "上架成功", Toast.LENGTH_SHORT).show();
                list.get(clickPosition).setIsSell(1);
                adapter.notifyDataSetChanged();
            } else if (xjSpBean.getData().getStatus().equals("0")) {
                Toast.makeText(this, "下架成功", Toast.LENGTH_SHORT).show();
                list.get(clickPosition).setIsSell(0);
                adapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void getMySpData(MySpBean mySpBean) {
        if (mySpBean.getState() == 0) {
            Toast.makeText(this, "数据加载失败", Toast.LENGTH_SHORT).show();
        } else if (mySpBean.getState() == 1) {
            list.clear();
            list.addAll(mySpBean.getData());
            if (list.size() == 0) {
                noFabu.setVisibility(View.VISIBLE);
                recyclerView.setVisibility(View.GONE);
            } else {
                noFabu.setVisibility(View.GONE);
                recyclerView.setVisibility(View.VISIBLE);
            }
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        spPresenter.getMySpData(SpfUtils.getSpfUtils(this).getToken());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (spPresenter != null) {
            spPresenter = null;
        }
        if (deleteSpPresenter != null) {
            deleteSpPresenter = null;
        }
        if (xjSpPresneter != null) {
            xjSpPresneter = null;
        }
        if (typePresenter != null) {
            typePresenter = null;
        }
    }

    @Override
    public void onRefresh() {
        spPresenter.getMySpData(SpfUtils.getSpfUtils(this).getToken());
        recyclerView.completeRefresh();
    }

    @Override
    public void onLoadMore() {

    }

}
